import axios from 'axios';
import { message } from 'tdesign-vue'
import proxy from '../config/host';


 const env = import.meta.env.MODE || 'development';

 const API_HOST = env === 'mock' ? '/' : proxy[env].API; // 如果是mock模式 就不配置host 会走本地Mock拦截

// const CODE = {
//   LOGIN_TIMEOUT: 1000,
//   REQUEST_SUCCESS: 0,
//   REQUEST_FOBID: 1001,
// };
let baseURL = "http://47.113.222.208:19000"; //线下
let config = {
  baseURL: baseURL,
  withCredentials: true, // 跨域
  timeout: 30000, //设置最大请求时间
  contentType: "application/json",
};
const instance = axios.create(config);

// eslint-disable-next-line
// @ts-ignore
// axios的retry ts类型有问题
instance.interceptors.retry = 3;
// 请求拦截器
instance.interceptors.request.use((config) =>{ 
   let token = "eyJhbGciOiJIUzUxMiJ9.eyJyb2xlcyI6WyJTVVBFUl9BRE1JTiJdLCJzdWIiOiIxNDU4OTQ1NTEyNiIsImlhdCI6MTcyNDYzODM5NywiZXhwIjoxNzMzMjc4Mzk3fQ.JZxdtJ-c3UkdlIz1In5n7XwTpGuCFHfCeexWGxvRVN5xhD3_H0vHa7DbxG1vLuG0GRJU3QEe98YYQkVVvEKi6A";
if (token) {
  // 在headers里面调用set方法设置一个Authorization属性
  config.headers.set( 'Authorization', token)
}
   return config
});
// 响应拦截器
instance.interceptors.response.use(
  (response) => {
    if (response.status === 200) {
      return Promise.resolve(response);
    } else {
      return Promise.reject(response);
    }
  },
  (error) => {
   // 状态码判断处理
   if (error.response.status == 500) {
    message.error("请求服务器错误");
  } else if (error.response.status == 401) {
    debugger
    message.error("token过期，请重新登录");
    // setTimeout(()=>{
    //   window.location.href= 'https://sys.ycyd777.com/login'
    // },1500)
    // this.$router.path("/login");
  } else {
    message.error(`'连接错误'${error.response.status}`);
  }
  return Promise.reject(error);
  },
);
//封装post,get方法
const myaxios = {
  get(url = "", params = {}) {
    return new Promise((resolve, reject) => {
      instance({
        url,
        params,
        headers: { "Content-Type": "application/json;charset=UTF-8" },
        method: "GET",
      })
        .then((res) => {
          resolve(res.data);
          return res;
        })
        .catch((error) => {});
    });
  },
  post(url = "", params = {}) {
    return new Promise((resolve, reject) => {
      instance({
        url,
        data: params,
        headers: { "Content-Type": "application/json;charset=UTF-8" },
        method: "POST",
      })
        .then((res) => {
          resolve(res.data);
          return res;
        })
        .catch((error) => {});
    });
  },
};
export default myaxios;
